// Copyright (C) 2004-2009
// Alex Turbov, Igor Veresov
//
//  Distributed under the Boost Software License, Version 1.0. (See accompanying
//  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

#include "custom_matrix.hpp"

#include <boost/thread/unilock.hpp>

#define BOOST_TEST_DYN_LINK
#include <boost/test/unit_test.hpp>

using namespace boost;

BOOST_AUTO_TEST_CASE(test_unilock_default_ex_0)
{
    typedef unilock<default_scheduler<exclusive_lock> > lock_t;
    lock_t l;

    l.lock(/*exclusive_lock::lock*/);
    BOOST_CHECK_EQUAL(l.try_lock(), false);
    l.unlock(/*exclusive_lock::lock*/);

    l.lock(exclusive_lock::lock);
    BOOST_CHECK_EQUAL(l.try_lock(exclusive_lock::lock), false);
    l.unlock(exclusive_lock::lock);
}

BOOST_AUTO_TEST_CASE(test_unilock_default_rw_0)
{
    typedef unilock<default_scheduler<rw_lock> > lock_t;
    lock_t l;

    l.lock(rw_lock::read);
    BOOST_CHECK_EQUAL(l.try_lock(rw_lock::read), true);
    l.unlock(rw_lock::read);
    l.unlock(rw_lock::read);

    l.lock(rw_lock::write);
    BOOST_CHECK_EQUAL(l.try_lock(rw_lock::write), false);
    l.unlock(rw_lock::write);
}

BOOST_AUTO_TEST_CASE(test_unilock_default_c_0)
{
    typedef unilock<default_scheduler<tests::custom_matrix> > lock_t;
    lock_t l;

    l.lock(tests::custom_matrix::lock_one);
    BOOST_CHECK_EQUAL(l.try_lock(tests::custom_matrix::lock_one), false);
    BOOST_CHECK_EQUAL(l.try_lock(tests::custom_matrix::lock_two), true);
    BOOST_CHECK_EQUAL(l.try_lock(tests::custom_matrix::lock_both), false);
    l.unlock(tests::custom_matrix::lock_one);
    l.unlock(tests::custom_matrix::lock_two);

    l.lock(tests::custom_matrix::lock_two);
    BOOST_CHECK_EQUAL(l.try_lock(tests::custom_matrix::lock_one), true);
    BOOST_CHECK_EQUAL(l.try_lock(tests::custom_matrix::lock_two), false);
    BOOST_CHECK_EQUAL(l.try_lock(tests::custom_matrix::lock_both), false);
    l.unlock(tests::custom_matrix::lock_one);
    l.unlock(tests::custom_matrix::lock_two);

    l.lock(tests::custom_matrix::lock_both);
    BOOST_CHECK_EQUAL(l.try_lock(tests::custom_matrix::lock_one), false);
    BOOST_CHECK_EQUAL(l.try_lock(tests::custom_matrix::lock_two), false);
    BOOST_CHECK_EQUAL(l.try_lock(tests::custom_matrix::lock_both), false);
    l.unlock(tests::custom_matrix::lock_both);
}
